package com.xrj4j.k_tally

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.xrj4j.k_tally.navigation.BottomNav
import com.xrj4j.k_tally.navigation.TallyNavHost
import com.xrj4j.k_tally.ui.theme.K_tallyTheme
import com.xrj4j.k_tally.view.viewmodel.EditViewModel

val LocalNavController =
    staticCompositionLocalOf<NavController> { noLocalProvidedFor("navController") }
val LocalEditViewModel =
    compositionLocalOf<EditViewModel> { noLocalProvidedFor("editViewModel") }

private fun <T> noLocalProvidedFor(name: String): T {
    error("CompositionLocal $name not present")
}

class MainActivity : ComponentActivity() {

    private val todoViewModel by viewModels<EditViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            K_tallyTheme {
                AppScreen(Modifier.padding(16.dp))
            }
        }
    }


    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    fun AppScreen(modifier: Modifier = Modifier) {

        CompositionLocalProvider(
            LocalNavController provides rememberNavController(),
            LocalEditViewModel provides todoViewModel
        ) {
            Scaffold(
                bottomBar = {
                    BottomNav()
                }
            ) {
                TallyNavHost(modifier = modifier.padding(it))
            }
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun TestPreview() {
        K_tallyTheme {
            AppScreen()
        }
    }
}



